﻿using aliyun_api_gateway_sdk.Constant;
using aliyun_api_gateway_sdk.Util;
using fuzhu;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace aliyun_api_gateway_sdk
{
   public class AliWeb
    {
        private const String appKey = "203949814";
        private const String appSecret = "sadFDxH7BF6gXVLCGBk2HvZa1NaarRgs";
        private const String host = "https://api.whchem.com";

        public static string Post(string bobyContent, string path,string apienv)
        {

            //String bobyContent = GetBody();
            //String bobyContent = "{\"vendor\": \"606272\", \"item\": [ { \"material\": \"80405361\",  \"availableQuantity\": \"12\",\"lockedQuantity\": \"3\",\" unit \": \"TAI\"  }  ]}";

            //String path = "/MM/00882/VendorSynchronizeInventory";
            Dictionary<String, String> headers = new Dictionary<string, string>();
            Dictionary<String, String> querys = new Dictionary<string, string>();
            Dictionary<String, String> bodys = new Dictionary<string, string>();
            List<String> signHeader = new List<String>();

            //设定Content-Type，根据服务器端接受的值来设置
            headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
            //设定Accept，根据服务器端接受的值来设置
            headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON);

            //注意：如果有非Form形式数据(body中只有value，没有key)；如果body中是key/value形式数据，不要指定此行
            headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(bobyContent)));
            //如果是调用测试环境请设置
            headers.Add(SystemHeader.X_CA_STAGE, apienv);

            ////注意：业务header部分，如果没有则无此行(如果有中文，请做Utf8ToIso88591处理)
            //headers.Add("b-header2", MessageDigestUtil.Utf8ToIso88591("headervalue1"));
            //headers.Add("a-header1", MessageDigestUtil.Utf8ToIso88591("headervalue2处理"));

            ////注意：业务query部分，如果没有则无此行；请不要、不要、不要做UrlEncode处理
            //querys.Add("b-query2", "queryvalue2");
            //querys.Add("a-query1", "queryvalue1");

            //注意：业务body部分
            bodys.Add("", bobyContent);

            //指定参与签名的header            
            signHeader.Add(SystemHeader.X_CA_TIMESTAMP);
            signHeader.Add("a-header1");
            signHeader.Add("b-header2");

            using (HttpWebResponse response = HttpUtil.HttpPost(host, path, appKey, appSecret, 30000, headers, querys, bodys, signHeader))
            {
                Console.WriteLine(response.StatusCode);
                Console.WriteLine(response.Method);
                Console.WriteLine(response.Headers);
                //Stream st = response.GetResponseStream();
                //StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                //Console.WriteLine(reader.ReadToEnd());
                //Console.WriteLine(Constants.LF);
                 
                var responseString = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")).ReadToEnd();
                //Console.WriteLine(responseString);
                //WriteLog write = new WriteLog();
                //write.WriteLogs(responseString);
                return responseString.ToString();


            }
        }

        public static string PostKuCun(string bobyContent, string apienv)
        {

            String path = "/MM/00882/VendorSynchronizeInventory";
            return Post(bobyContent, path, apienv);
        }

        public static string PostSuoDing(string bobyContent, string apienv)
        {

            String path = "/MM/00884/VendorFeedbackLockedInventory";
            return Post(bobyContent, path, apienv);
        }

        public static void doPostString2()
        {

            //String bobyContent = GetBody();
            String bobyContent = "{\"vendor\": \"606272\", \"item\": [ {\"PR\": \"1081111\", \"material\": \"511114\",  \"successfulQuantity\": \"\",\"failedQuantity\": null,\"PRitem\":null } ] }";

            String path = "/MM/00884/VendorFeedbackLockedInventory";
            Dictionary<String, String> headers = new Dictionary<string, string>();
            Dictionary<String, String> querys = new Dictionary<string, string>();
            Dictionary<String, String> bodys = new Dictionary<string, string>();
            List<String> signHeader = new List<String>();

            //设定Content-Type，根据服务器端接受的值来设置
            headers.Add(HttpHeader.HTTP_HEADER_CONTENT_TYPE, ContentType.CONTENT_TYPE_JSON);
            //设定Accept，根据服务器端接受的值来设置
            headers.Add(HttpHeader.HTTP_HEADER_ACCEPT, ContentType.CONTENT_TYPE_JSON);

            //注意：如果有非Form形式数据(body中只有value，没有key)；如果body中是key/value形式数据，不要指定此行
            headers.Add(HttpHeader.HTTP_HEADER_CONTENT_MD5, MessageDigestUtil.Base64AndMD5(Encoding.UTF8.GetBytes(bobyContent)));
            //如果是调用测试环境请设置
            headers.Add(SystemHeader.X_CA_STAGE, "TEST");

            ////注意：业务header部分，如果没有则无此行(如果有中文，请做Utf8ToIso88591处理)
            //headers.Add("b-header2", MessageDigestUtil.Utf8ToIso88591("headervalue1"));
            //headers.Add("a-header1", MessageDigestUtil.Utf8ToIso88591("headervalue2处理"));

            ////注意：业务query部分，如果没有则无此行；请不要、不要、不要做UrlEncode处理
            //querys.Add("b-query2", "queryvalue2");
            //querys.Add("a-query1", "queryvalue1");

            //注意：业务body部分
            bodys.Add("", bobyContent);

            //指定参与签名的header            
            signHeader.Add(SystemHeader.X_CA_TIMESTAMP);
            signHeader.Add("a-header1");
            signHeader.Add("b-header2");

            using (HttpWebResponse response = HttpUtil.HttpPost(host, path, appKey, appSecret, 30000, headers, querys, bodys, signHeader))
            {
                Console.WriteLine(response.StatusCode);
                Console.WriteLine(response.Method);
                Console.WriteLine(response.Headers);
                Stream st = response.GetResponseStream();
                StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
                Console.WriteLine(reader.ReadToEnd());
                Console.WriteLine(Constants.LF);

            }
        }
     

       
    }
}
